package com.hk.Mgr.web.controller.app.yingmi;

import com.hk.api.ProcessAPI;
import com.hk.app.utils.YingmiOpenAPI;
import com.hk.base.BaseCtrl;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.vo.JsonResult;
import com.hk.yingmi.YingmiOpenAPIConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Antinomy on 17/8/31.
 */
@Controller
@RequestMapping(value = BaseCtrl.App)
public class YingmiSurveyController {

    @Autowired
    private YingmiOpenAPI openAPI;

    @RequestMapping(value = YingmiOpenAPIConstant.YINGMI_API_ACCOUNT_GET_SURVEY, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getSurvey(HttpServletRequest request, Long userId) {

        ProcessAPI api = (jsonResult, validate) -> {

            openAPI.getValidatedUser(userId, validate);

            String jsonResp = openAPI.sslGet(YingmiOpenAPIConstant.ACCOUNT_GET_SURVEY, null, null);
            openAPI.processJsonResult(jsonResult, jsonResp);
        };

        return api.process(request);
    }

    @RequestMapping(value = YingmiOpenAPIConstant.YINGMI_API_ACCOUNT_ANSWER_SURVEY, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult answerSurvey(HttpServletRequest request, String brokerUserId, String accountId,String selections) {
        ProcessAPI api = (jsonResult, validate) -> {

            validate.check("brokerUserId", brokerUserId);
            validate.check("accountId", accountId);
            validate.check("selections", selections);

            Map<String, String> params = new HashMap<>();
            params.put("brokerUserId", brokerUserId);
            params.put("accountId", accountId);
            params.put("selections", selections);

            String result = openAPI.sslPost(YingmiOpenAPIConstant.ACCOUNT_ANSWER_SURVEY, params);

            openAPI.processJsonResult(jsonResult, result);
        };

        return api.process(request);

    }

    @RequestMapping(value = YingmiOpenAPIConstant.YINGMI_API_ACCOUNT_SET_RISK_GRADE, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult setRiskGrade(HttpServletRequest request, String brokerUserId, String accountId,String riskGrade) {
        ProcessAPI api = (jsonResult, validate) -> {

            validate.check("brokerUserId", brokerUserId);
            validate.check("accountId", accountId);
            validate.check("riskGrade", riskGrade);

            Map<String, String> params = new HashMap<>();
            params.put("brokerUserId", brokerUserId);
            params.put("accountId", accountId);
            params.put("riskGrade", riskGrade);

            String result = openAPI.sslPost(YingmiOpenAPIConstant.ACCOUNT_SET_RISK_GRADE, params);

            openAPI.processJsonResult(jsonResult, result);
        };

        return api.process(request);

    }
}
